package com.example.demo.allot;

import cn.creditease.std.consumer.AbstactJobConsumeWorker;
import com.alibaba.fastjson.JSON;
import com.example.demo.entity.UserEntity;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * @Description:
 * @Author: kai.yang
 * @Date: 2019-08-13 10:15
 */
@Component
public class TestAllotProcessor extends AbstactJobConsumeWorker<UserEntity> {

    private static final Logger logger = LoggerFactory.getLogger(TestAllotProcessor.class);

    public List<UserEntity> doReader(int pageNo, int pageSize) {
        logger.info("处理job数据{},{}", pageNo, pageSize);
        //模拟数据返回
        //从数据库读取pageNo页数据PageSize条
        List<UserEntity> userEntities = new ArrayList<UserEntity>();
        for (int i = pageNo * pageSize; i < (pageNo + 1) * pageSize; i++) {
            userEntities.add(new UserEntity(i, "开心" + i + "号", i));
        }

        return userEntities;
    }

    public boolean doProcess(List<UserEntity> datas) {
        logger.info("处理job业务数据{}", JSON.toJSONString(datas));
        for (UserEntity u : datas) {
            //模拟处理，将所有人成绩加10分
            u.setGrade(u.getGrade() + 10);
        }
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return true;
    }

    public String getJobName() {
        return "test";
    }
}
